package com.schoolhostptail.controller;

import com.schoolhostptail.entity.Student;
import com.schoolhostptail.entity.Teacher;
import com.schoolhostptail.entity.view.DoctorModel;
import com.schoolhostptail.entity.view.StudentModel;
import com.schoolhostptail.entity.view.TeacherModel;
import com.schoolhostptail.qpage.QPage;
import com.schoolhostptail.service.IStudentService;
import com.schoolhostptail.service.ITeacherService;
import com.schoolhostptail.util.ExcelUtil;
import com.schoolhostptail.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 教师控制层入口
 */
@Controller
@RequestMapping("/api/teacher")
public class TeacherController {

    /**
     * 业务层
     */
    @Autowired
    private ITeacherService service;

    /**
     * 路由地址
     */
    public String prefix = "/admin/teacher";


    /**
     * 进入列表页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


    /**
     * 获取所有的列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result getList(QPage qPage, Teacher obj, HttpSession session) {
        return Result.reOk("查询成功",this.service.getList(qPage,obj));
    }


    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return prefix+"/add";
    }

    /**
     * 新增保存
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Teacher obj) {
        return this.service.insert(obj);
    }

    /**
     * 进入编辑页面
     * @return
     */
    @RequestMapping("/edit")
    public String edit(){
        return prefix+"/edit";
    }

    /**
     * 根据ID获取信息
     * @param id
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public Result getById(Integer id) {
        return Result.reOk("查询成功",this.service.getById(id));
    }

    /**
     * 编辑保存
     * @return
     */
    @RequestMapping("/updates")
    @ResponseBody
    public Result updates(Teacher obj) {
        return this.service.updates(obj);
    }


    /**
     * 删除保存
     * @return
     */
    @RequestMapping("/deleteId")
    @ResponseBody
    public Result deleteId(Integer id) {
        return this.service.deleteId(id);
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping("/importExcel")
    @ResponseBody
    public Result importExcel(@RequestParam("uploadfile") MultipartFile files) throws IOException {
        if (files.isEmpty()) {
            return Result.reError("抱歉，系統检查文件没有内容，或者为损坏状态，导入失败!!!");
        }
        List<TeacherModel> ulist=  ExcelUtil.syncReadModel(files.getInputStream(), TeacherModel.class,0,1);
        if (ulist == null || ulist.size() <= 0) {
            return Result.reError("此文件没有解析可导入的数据，导入失败");
        }
        return this.service.importExcel(ulist);
    }

}
